/**
 * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cnguifang.feigewallet.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.cnguifang.feigewallet.R;
import com.cnguifang.feigewallet.data.result.SmsCode;
import com.cnguifang.feigewallet.network.AppNetworkUtils;
import com.cnguifang.feigewallet.utils.AccountValidatorUtil;
import com.cnguifang.feigewallet.utils.UserInfoUtils;
import com.mathum.baseapp.network.NetworkCallback;
import com.mathum.baseapp.network.NetworkUtils;

/**
 * register screen
 */
public class Register1Activity extends BaseActivity {
    private EditText userNameEditText;
//    private EditText passwordEditText;
    private EditText confirmPwdEditText;
    private TextView tvCount;
    private String msgCode;
    private String userPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.em_activity_register2);
        userNameEditText = (EditText) findViewById(R.id.username);
//        passwordEditText = (EditText) findViewById(R.id.password);
        confirmPwdEditText = (EditText) findViewById(R.id.sms_code);
        tvCount = (TextView) findViewById(R.id.tv_count);
        tvCount.setOnClickListener(view -> {
            if (TextUtils.isEmpty(userNameEditText.getText().toString())) {
                Toast.makeText(Register1Activity.this, "手机号码不能为空", Toast.LENGTH_LONG).show();
                return;
            }
//            if (TextUtils.isEmpty(passwordEditText.getText().toString())) {
//                Toast.makeText(RegisterActivity.this, "密码不能为空", Toast.LENGTH_LONG).show();
//                return;
//            }
            if (!AccountValidatorUtil.isMobile(userNameEditText.getText().toString())) {
                Toast.makeText(Register1Activity.this, "手机格式错误", Toast.LENGTH_LONG).show();
                return;
            }
            tvCount.setText("已发送");
            NetworkUtils.fetchInfo(AppNetworkUtils.initRetrofitApi().fetchSmsCode(userNameEditText.getText().toString()),
                    new NetworkCallback<SmsCode>() {
                        @Override
                        public void onSuccess(SmsCode baseData) {
                            msgCode=baseData.getCodemsg().trim();
//                            confirmPwdEditText.setText(baseData.getCodemsg().trim());
                        }

                        @Override
                        public void onFailure(Throwable t) {
                            Toast.makeText(Register1Activity.this, "网络异常", Toast.LENGTH_LONG).show();
                        }
                    });
        });
        userPhone=getIntent().getStringExtra("userPhone");
        if(userPhone!=null){
            userNameEditText.setText(UserInfoUtils.getPhone(Register1Activity.this));
            userNameEditText.setEnabled(false);
        }
    }

    public void register(View view) {
        final String usermobile = userNameEditText.getText().toString().trim();
//        final String pwd = passwordEditText.getText().toString().trim();
        String confirm_pwd = confirmPwdEditText.getText().toString().trim();
        if (TextUtils.isEmpty(usermobile)) {
            Toast.makeText(this, getResources().getString(R.string.User_mobile_cannot_be_empty), Toast.LENGTH_SHORT).show();
            userNameEditText.requestFocus();
            return;
        }
//        else if (TextUtils.isEmpty(pwd)) {
//            Toast.makeText(this, getResources().getString(R.string.Password_cannot_be_empty), Toast.LENGTH_SHORT).show();
//            passwordEditText.requestFocus();
//            return;
//        }
        else if (TextUtils.isEmpty(confirm_pwd)) {
            Toast.makeText(this, "验证码不能为空!", Toast.LENGTH_SHORT).show();
            confirmPwdEditText.requestFocus();
            return;
        }else if (TextUtils.isEmpty(msgCode)) {
            Toast.makeText(this, "请先获取验证码!", Toast.LENGTH_SHORT).show();
            confirmPwdEditText.requestFocus();
            return;
        }

        if (!TextUtils.isEmpty(usermobile) && !TextUtils.isEmpty(confirm_pwd)) {
            if(confirm_pwd.equals(msgCode)){
                if(userPhone==null){
                Intent intent = new Intent(Register1Activity.this,
                        RegisterPasswordActivity.class);
                intent.putExtra("user_mobile",usermobile);
                intent.putExtra("user_msgcode",msgCode);
                startActivityForResult(intent,1);
            }else{
                    setResult(Activity.RESULT_OK, getIntent());
                    finish();
                }
            }

        }
    }

    public void back(View view) {
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
            finish();
        }
    }
}
